const { exec } = require("../db/mysql")

const getList = (author, keyword) => {
    let sql = `select * from blogs where 1=1 `
    if (author) {
        sql += `and author='${author}' `
    }
    if (keyword) {
        sql += `and title like '%${keyword}'% `
    }
    sql += `order by createtime desc;`

    return exec(sql)
}

const getDetail = (id) => {
    const sql = `select * from blogs where id='${id}'`
    return exec(sql).then(rows => {
        return rows[0]
    })

    // return [
    //     {
    //         id: 1,
    //         title: 'asd',
    //         content: 'content1',
    //         author: 'author1',
    //         createTime: 'createTime1'
    //     },
    //     {
    //         id: 2,
    //         title: 'asd2',
    //         content: 'content2',
    //         author: 'author2',
    //         createTime: 'createTime2'
    //     }
    // ]
}

const newBlog = (blogData = {}) => {
    return [
        {
            id: 3,
        },
    ]
}

const updateBlog = (id, blogData = {}) => {
    return true
}

const delBlog = (id,) => {
    console.log('id, blogData', id, blogData);
    return true
}

module.exports = {
    getList,
    getDetail,
    newBlog,
    updateBlog,
    delBlog
}